package ShareLife.gar;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;


public class PlayerInfo {
	public int ID = 0;
	public String name = null;
	public int xposition = 0;
	public int yposition = 0;
	public int phonenumber = 0;
	public Bitmap photoImage = null;
	public Bitmap myContent = null;
	public boolean visbile;
	public Bitmap myreferenceImage=null;
	public int titlecolor=0x90;
	public int backgroundcolor=0xff;
	public static int LENGTH = 100;
	public String myName;
	public String phoneNumber;
	public Vector<FriendItem> FriendList;
	
	public long facebookUserId;
	
	
	public PlayerInfo(String aName, int aID, int aXposition, int aYposition,
			Bitmap aPhotoImage,int color,String aPhoneNumber) {
		name = aName;
		ID = aID;
		xposition = aXposition;
		yposition = aYposition;
		photoImage = aPhotoImage;
		myreferenceImage = aPhotoImage;
		myContent = Bitmap.createBitmap(LENGTH, LENGTH, Config.ARGB_8888);
		Matrix mMatrix = new Matrix();
		float tempwidth = aPhotoImage.getWidth();
		float tempheight = aPhotoImage.getHeight();
		mMatrix.postScale((LENGTH / tempwidth), LENGTH
				/ tempheight);
		mMatrix.postTranslate(0, 30);
		myName = aName;
		Canvas background = new Canvas();
		background.setBitmap(myContent);
		Paint backpaint = new Paint();
		titlecolor = titlecolor<<24 | color;
		backgroundcolor = backgroundcolor<<24 | color;
		backpaint.setColor(titlecolor);
		background.drawRect(0,0, 100, 15, backpaint);
		backpaint.setColor(backgroundcolor);
		background.drawRect(0, 15, 100, 30, backpaint);
		backpaint.setColor(Color.argb(0xff, 0xff, 0xff, 0xff));
		background.drawText(aName,(100 - backpaint.measureText(aName))/2,12,backpaint);
		Log.i("stringphonenumber","number"+aPhoneNumber);
		if(aPhoneNumber!= null)
			background.drawText(aPhoneNumber,(100 - backpaint.measureText(aPhoneNumber))/2 , 27, backpaint);
		
		background.drawBitmap(photoImage, mMatrix, backpaint);
		mMatrix.reset();
		phoneNumber = aPhoneNumber;
		Random r = new Random();
		float randomSkewY = Math.abs((float) ((r.nextInt())) % 3 / 50);
		if (aID % 2 != 0) {
			randomSkewY = randomSkewY * (-1);

		}
		mMatrix.setSkew(0.0f, randomSkewY);
		photoImage = Bitmap.createBitmap(myContent, 0, 0, myContent.getWidth(),
				myContent.getHeight(), mMatrix, true);
		aPhotoImage = null;
		r = null;
		mMatrix = null;
	}

	public void setLocation(int aX, int aY) {
		xposition = aX;
		yposition = aY;
	}
	public void writeinfor(DataOutputStream dos) throws IOException {
		if(phoneNumber == null)
		{
			phoneNumber = "10086";
		}
		if(name == null)
		{
			name = "steven";
		}
		
		dos.writeLong(ToolOld.formatPhoneNumber(phoneNumber));
		dos.writeUTF(name);
		int tempWidth = myreferenceImage.getWidth();
		int tempHeight = myreferenceImage.getHeight();
		int imagedata[] = new int[tempWidth*tempHeight];
		myreferenceImage.getPixels(imagedata, 0,tempWidth , 0, 0, tempWidth, tempHeight);
		byte photoData[] = ToolOld.int2byte(imagedata);
		dos.writeInt(tempWidth);
		dos.writeInt(tempHeight);
		ToolOld.sendByteArray(dos,photoData);	
	}
	
	
	public void getinfor(DataInputStream dis) throws IOException {
		
		FriendList = new Vector<FriendItem>();
		int Friendnumber = dis.readInt();
		for(int i=0; i < Friendnumber; i++)
		{
			FriendItem tempFriend = new FriendItem(this);
			tempFriend.readinfor(dis);
			FriendList.addElement(tempFriend);
		}	
		
	}
	public void releaseFriendList()
	{
		FriendList.removeAllElements();
		FriendList = null;
	}
	public void drawSelf(Canvas aGraphics, Paint aPaint) {
		if (photoImage != null)
			ToolOld.drawImage(aGraphics, photoImage, xposition, yposition);
		else
			aGraphics.drawRect(xposition, yposition, xposition + 30,
					yposition + 40, aPaint);
	}

	
	
	public PlayerInfo(int aId) {
		ID = aId;
		phoneNumber=Long.toString(aId);
	}

	
	public void readinfor(DataInputStream dis) throws IOException {
		String strNum = dis.readUTF();
		name = dis.readUTF();
		int photowidth = dis.readInt();
		int photoheight = dis.readInt();
		int imagesize = ((photowidth * photoheight) << 2);

		byte[] photoData = new byte[imagesize];
		receiveByteArray(dis, photoData);

		int[] temparray = ToolOld.byte2int(photoData);
		photoImage = Bitmap.createBitmap(temparray, photowidth, photoheight, Config.ARGB_8888);
		myreferenceImage = photoImage;

		Canvas canvas = new Canvas();
		myContent = Bitmap.createBitmap(LENGTH, LENGTH, Config.ARGB_8888);
		canvas.setBitmap(myContent);
		
		
		Paint paint = new Paint();

		paint.setColor(0x90 << 24 | 0x00ff0000);
		canvas.drawRect(0, 0, 100, 15, paint);
		paint.setColor(0xff << 24 | 0x00ff0000);
		canvas.drawRect(0, 15, 100, 30, paint);
		
		paint.setColor(Color.argb(0xff, 0xff, 0xff, 0xff));
		canvas.drawText(name, (100 - paint.measureText(name)) / 2, 12, paint);
		canvas.drawText(phoneNumber, (100 - paint.measureText(phoneNumber)) / 2, 27, paint);

		Matrix matrix = new Matrix();
		matrix.postScale((LENGTH / photoImage.getWidth()), LENGTH / photoImage.getHeight());
		matrix.postTranslate(0, 30);
		
		canvas.drawBitmap(photoImage, matrix, paint);

		photoImage = Bitmap.createBitmap(myContent, 0, 0, myContent.getWidth(), myContent.getHeight(), matrix, true);
	}

	
	public void receiveByteArray(DataInputStream dis, byte Data[]) {
		int currentIndex = 0;
		int storebytenumber = 0;
		while (currentIndex != Data.length) {
			storebytenumber = Data.length - currentIndex >= 512 ? 512 : Data.length - currentIndex;
			byte temparray[] = new byte[storebytenumber];
			try {
				dis.read(temparray, 0, storebytenumber);
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.arraycopy(temparray, 0, Data, currentIndex, storebytenumber);
			currentIndex += storebytenumber;
		}
	}
}
